//
//  BaseViewController.m
//  Trackit
//
//  Created by yaodaobin on 2017/4/2.
//  Copyright © 2017年 yaodaobin. All rights reserved.
//

#import "BaseViewController.h"
#import "LoginViewController.h"

#define WAITING_ICON_SIZE 60


@interface BaseViewController ()
@end

@implementation BaseViewController

+ (UIViewAnimationOptions)animationOptionsForCurve:(UIViewAnimationCurve)curve {
    
    switch (curve) {
        case UIViewAnimationCurveEaseInOut:
            return UIViewAnimationOptionCurveEaseInOut;
            break;
        case UIViewAnimationCurveEaseIn:
            return UIViewAnimationOptionCurveEaseIn;
            break;
        case UIViewAnimationCurveEaseOut:
            return UIViewAnimationOptionCurveEaseOut;
            break;
        case UIViewAnimationCurveLinear:
            return UIViewAnimationOptionCurveLinear;
            break;
    }
    
    return kNilOptions;
}

#pragma mark - Controller LifeCycle
- (void)dealloc {
    DLog(@"======dealloc %@",self);
    if (self.isViewLoaded) {
        [self.view removeGestureRecognizer:_tapGesture];
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
            self.edgesForExtendedLayout = UIRectEdgeNone;
            self.extendedLayoutIncludesOpaqueBars = NO;
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    if (self.navigationController != nil && self.navigationController.childViewControllers.count > 1) {
        UIBarButtonItem *backButton = [UIBarButtonItem barItemWithImage:[UIImage imageNamed:kMainBackBtnImgName] target:self action:@selector(back)];
        self.navigationItem.leftBarButtonItem = backButton;
    }
    //点击空白 收起键盘
    _tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
    _tapGesture.cancelsTouchesInView =  NO;
    _tapGesture.delegate = self;
//    [self.view addGestureRecognizer:_tapGesture];

}

#pragma mark - Public
- (void)initParams {
    
}

- (void)initUI {

}

- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setCustomTitle:(NSString *)title {    
    self.navLabel.text = title;
    [self.navLabel sizeToFit];
    self.navigationItem.titleView = self.navLabel;
}

- (UINavTitleLabel *)navLabel {
    if (!_navLabel) {
        _navLabel = [[UINavTitleLabel alloc] initWithFrame:CGRectZero];
        _navLabel.clipsToBounds = YES;
        _navLabel.backgroundColor = [UIColor clearColor];
    }
    return _navLabel;
}

#pragma mark - Loading
- (void)showWaitingIcon:(CGFloat)verticalOffset {
    if (!_waitingIcon) {
        _waitingIcon = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WAITING_ICON_SIZE, WAITING_ICON_SIZE)];
        [_waitingIcon setCenter:CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2 - WAITING_ICON_SIZE / 2 + verticalOffset)];
        [_waitingIcon setBackgroundColor:[UIColor blackColor]];
        [_waitingIcon setAlpha:0.6];
        [_waitingIcon.layer setCornerRadius:5];
        _waitingIcon.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
        [self.view addSubview:_waitingIcon];
        
        UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        [indicatorView startAnimating];
        indicatorView.tag = 3125;
        [indicatorView setFrame:CGRectMake(WAITING_ICON_SIZE / 4, WAITING_ICON_SIZE / 4, WAITING_ICON_SIZE / 2, WAITING_ICON_SIZE / 2)];
        [_waitingIcon addSubview:indicatorView];
    }
    
    _waitingIcon.hidden = NO;
    UIActivityIndicatorView *indicatorView = (UIActivityIndicatorView *)[_waitingIcon viewWithTag:3125];
    if (indicatorView) {
        [indicatorView startAnimating];
    }
    [self.view bringSubviewToFront:_waitingIcon];
}

- (void)showWaitingIcon {
    _reloading = YES;
    [self showWaitingIcon:0];
}

- (void)dismissWaitingIcon {
    _reloading = NO;
    _waitingIcon.hidden = YES;
}

#pragma mark - Login
- (BOOL)needLogin {
    BOOL isLogin = self.isLogin;
    if (!isLogin) {
        [self login];
    }
    return !isLogin;
}

- (BOOL)needLogin:(LoginResult_block)block {
    BOOL isLogin = self.isLogin;
    if (!isLogin) {
        [self login:block];
    }
    return !isLogin;
}

- (void)needLoginDo:(void (^)())block {
    if (![self needLogin:^(NSDictionary *data, NSError *error, BOOL loginSuccess) {
        if (loginSuccess) {
            block();
        }
    }]) {
        block();
    }
    
}

- (void)login:(LoginResult_block)block {
    PresentedNavigationController *loginNavController = APP_DELEGATE.loginNavController;
    if (loginNavController == nil) {
        LoginViewController *vc = [[LoginViewController alloc] init];
        vc.loginResult_block  = block;
        loginNavController = [[PresentedNavigationController alloc] initWithRootViewController:vc];
        APP_DELEGATE.loginNavController = loginNavController;
    }
    
    loginNavController.rootViewController = self;
    [loginNavController present];
    
}

- (void)login {
    [self needLogin:nil];
}

#pragma mark - Gesture
- (void)tapGestureAction:(id)sender {
    [self hideKeyboard];
}

- (void)hideKeyboard {
    if (self.viewLoaded) {
        [self.view endEditing:YES];
    }
}

#pragma mark - Private
- (BOOL)isLogin {
    return APP_DELEGATE.user.userID.length > 0;
}

@end
